﻿Partial Public Class AlbumBuilderControl

    Private Sub AlbumBuilderControl_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        If System.ComponentModel.DesignerProperties.GetIsInDesignMode(Me) Then Return
        If g_album_mgr Is Nothing OrElse g_album_mgr.CurrentAlbum Is Nothing Then Return

        AddHandler g_album_mgr.AlbumLoaded, AddressOf OnAlbumLoaded
        AddHandler g_album_mgr.CurrentAlbumChanged, AddressOf OnCurrentAlbumChanged
        LoadAlbumInfo(g_album_mgr.CurrentAlbum)
    End Sub

    Public Sub SetQuery(ByVal sql As String, ByVal queryname As String)
        If queryname <> "" Then txtAlbumName.Text = queryname
        txtQuery.Text = sql
    End Sub

    Private Delegate Sub DlgOnAlbumLoaded()
    Private Sub OnAlbumLoaded()
        If Not Me.Dispatcher.CheckAccess() Then
            Me.Dispatcher.Invoke(Windows.Threading.DispatcherPriority.Normal, New DlgOnAlbumLoaded(AddressOf OnAlbumLoaded))
        Else
            Try
                lblAlbumStatus.Content = g_album_mgr.Photos.Rows.Count & " photo(s) loaded."
            Catch ex As Exception
                lblAlbumStatus.Content = "Album not loaded."
            End Try
        End If
    End Sub

    Private Sub OnCurrentAlbumChanged(ByVal currentalbum As Core.Album)
        LoadAlbumInfo(currentalbum)
    End Sub

    Private Delegate Sub DlgLoadAlbumInfo(ByVal album As Album)
    Private Sub LoadAlbumInfo(ByVal album As Album)
        If Not Me.Dispatcher.CheckAccess() Then
            Me.Dispatcher.Invoke(Windows.Threading.DispatcherPriority.Normal, New DlgLoadAlbumInfo(AddressOf LoadAlbumInfo), album)
        Else
            txtQuery.Text = album.Query
            txtAlbumName.Text = album.AlbumName
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSave.Click
        Try
            If txtAlbumName.Text <> g_album_mgr.CurrentAlbum.AlbumName Then
                SaveAs()
            Else
                g_album_mgr.UpdateCurrentAlbum(txtQuery.Text)
                g_album_mgr.LoadAlbum()
            End If
        Catch ex As Exception
            ReportErrorQuery(ex)
        End Try
    End Sub


    Private Sub btnSaveAs_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSaveAs.Click
        SaveAs()
    End Sub
    Private Sub SaveAs()
        If g_album_mgr.ContainsAlbumName(txtAlbumName.Text) Then
            MessageBox.Show("Please enter a different album name", "Album exists", MessageBoxButton.OK, MessageBoxImage.Error)
            Return
        End If
        Try
            Dim newalbum As Album = Nothing
            newalbum = New Album(txtAlbumName.Text, txtQuery.Text)
            g_album_mgr.AddAlbum(newalbum)
            g_album_mgr.CurrentAlbum = newalbum
            g_album_mgr.LoadAlbum()
        Catch ex As Exception
            ReportErrorQuery(ex)
        End Try
    End Sub
End Class
